package ptu.controller;


import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import ptu.entity.Book;
import ptu.result.Result;
import ptu.service.BookService;

import java.util.List;

@RequestMapping("/book")
@Controller
public class BookController {
    @Autowired
    private BookService bookService;

    /**
     * 查询全部书籍
     * @param username
     * @return {@link String }
     */
    @GetMapping("/selectAll")
    @ResponseBody
    public Result selectAll(String username){
        return  bookService.selectAll();
    }

    /**
     * 模糊查询
     * @param work
     * @param model
     * @return {@link String }
     */
    @GetMapping("/select")
    public String select(String work,Model model){
        Result result = bookService.select(work);
        model.addAttribute(result);
        return "true";
    }

    /**
     * 添加图书
     * @param book
     * @return {@link String }
     */
    @PostMapping("/add")
    @ResponseBody
    public Result addBook(Book book){
        return bookService.addBook(book);
    }

    @GetMapping("/{id}")
    public String getBookById(@PathVariable Integer id,Model model) {
        Book book=bookService.getBookById(id);
        model.addAttribute("book", book);
        return "manager/book_update";
    }

    /**
     * 修改图书
     * @param book
     * @return {@link String }
     */
    @PostMapping("/update")
    @ResponseBody
    public Result update(Book book){
        return bookService.update(book);
    }

    /**
     * 删除图书
     * @param bName
     * @return {@link String }
     */
    @GetMapping("/delete/{name}")
    @ResponseBody
    public Result delete(@PathVariable("name") String bName){
        return bookService.delete(bName);
    }

    /**
     * 多条件查询
     * @param username
     * @param bName
     * @param bAuthor
     * @param bPress
     * @param model
     * @return {@link String }
     */
    @PostMapping("queryLike")
    public String queryLike(String username,String bName,String bAuthor,String bPress,Model model){
        Book book = new Book();
        book.setBName(bName);
        book.setBAuthor(bAuthor);
        book.setBPress(bPress);
        Result result = bookService.queryLike(book);
        model.addAttribute("books",(List<Book>)result.getData());
        model.addAttribute("username",username);
        return "user/borrowBook";
    }



}
